package com.ruoyi.logistics.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.logistics.domain.Center;
import com.ruoyi.logistics.domain.po.ExplicitRegion;
import com.ruoyi.logistics.mapper.CenterMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.logistics.mapper.RegionMapper;
import com.ruoyi.logistics.domain.Region;
import com.ruoyi.logistics.service.IRegionService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 详细地区Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-06
 */
@Service
public class RegionServiceImpl implements IRegionService 
{
    @Autowired
    private RegionMapper regionMapper;

    @Autowired
    private CenterMapper centerMapper;

    /**
     * 查询详细地区
     * 
     * @param id 详细地区主键
     * @return 详细地区
     */
    @Override
    public Region selectRegionById(Long id)
    {
        return regionMapper.selectRegionById(id);
    }

    /**
     * 查询详细地区包括下面的物流中心
     *
     * @param id 详细地区主键
     * @return 详细地区
     */
    @Override
    public Region selectExplicitRegionById(Long id)
    {
        return regionMapper.selectExplicitRegionById(id);
    }

    /**
     * 查询详细地区
     *
     * @param id 详细地区主键
     * @return 详细地区
     */
    @Override
    public Region selectRegionByParentCode(Long id)
    {
        /*判断是否是直辖市如果是,就返回该直辖市的对象*/
        Region region = regionMapper.selectRegionById(id);
        if (region.getParentCode()==0){
            System.out.println(region);
            return region;
        }
        Region regionByCode = regionMapper.selectRegionByCode(region.getParentCode());
        System.out.println(regionByCode);
        return regionByCode;
    }

    /**
     * 查询详细地区列表
     * 
     * @param region 详细地区
     * @return 详细地区
     */
    @Override
    public List<Region> selectRegionList(Region region)
    {
        return regionMapper.selectRegionList(region);
    }

    public List<ExplicitRegion> selectReginCenterList(Integer level){
        /*1级区域*/
        List<ExplicitRegion> explicitRegions = regionMapper.selectParentCodeList(new ExplicitRegion(0L));
        for (ExplicitRegion region : explicitRegions) {
            /*非直辖市*/
            if (region.getCode()!=110000 && region.getCode()!=120000 && region.getCode()!=310000 && region.getCode()!=500000){
                List<ExplicitRegion> list = regionMapper.selectParentCodeList(new ExplicitRegion(region.getCode()));
                /*3级区域*/
                if (level>2) {
                    for (ExplicitRegion explicitRegion : list) {
                        Center center = new Center(explicitRegion.getId());
                        /*物流中心*/
                        explicitRegion.setChildren(centerMapper.selectCenterList(center));
                    }
                }
                /*2级区域*/
                region.setChildren(list);
            }
            /*直辖市*/
            else {
                if (level>2){
                    Center center = new Center(region.getId());
                    region.setChildren(centerMapper.selectCenterList(center));
                }
            }
        }
        return explicitRegions;
    }

    /**
     * 新增详细地区
     * 
     * @param region 详细地区
     * @return 结果
     */
    @Transactional//当应用在方法上时，@Transactional注解会将该方法包装在一个事务中。如果方法执行成功，则事务会被提交；如果方法执行失败，则事务会被回滚。
    @Override
    public int insertRegion(Region region)
    {
        int rows = regionMapper.insertRegion(region);
        insertCenter(region);
        return rows;
    }

    /**
     * 修改详细地区
     * 
     * @param region 详细地区
     * @return 结果
     */
    @Transactional//当应用在方法上时，@Transactional注解会将该方法包装在一个事务中。如果方法执行成功，则事务会被提交；如果方法执行失败，则事务会被回滚。
    @Override
    public int updateRegion(Region region)
    {
        regionMapper.deleteCenterByRegionId(region.getId());
        insertCenter(region);
        return regionMapper.updateRegion(region);
    }

    /**
     * 批量删除详细地区
     * 
     * @param ids 需要删除的详细地区主键
     * @return 结果
     */
    @Override
    public int deleteRegionByIds(Long[] ids)
    {
        regionMapper.deleteCenterByRegionIds(ids);
        return regionMapper.deleteRegionByIds(ids);
    }

    /**
     * 删除详细地区信息
     * 
     * @param id 详细地区主键
     * @return 结果
     */
    @Override
    public int deleteRegionById(Long id)
    {
        regionMapper.deleteCenterByRegionId(id);
        return regionMapper.deleteRegionById(id);
    }

    /**
     * 新增物流中心信息
     *
     * @param region 区域对象
     */
    public void insertCenter(Region region)
    {
        List<Center> centerList = region.getCenterList();
        Long id = region.getId();
        if (StringUtils.isNotNull(centerList))
        {
            List<Center> list = new ArrayList<Center>();
            for (Center center : centerList)
            {
                center.setRegionId(id);
                list.add(center);
            }
            if (list.size() > 0)
            {
                regionMapper.batchCenter(list);
            }
        }
    }
}
